from django.core.management.base import BaseCommand
from backend.cews.models import RepairOrderTask
from backend.cews.signals import check_repair_order_task # noqa: F401
from django.db import transaction

class Command(BaseCommand):
    help = 'update parts order status'
    def handle(self, *args, **options):
        rots = RepairOrderTask.objects.all()
        for rot in rots:
            repair_order = rot.repair_order
            repair_order_part = rot.repair_order_part
            repair_order_part.status = repair_order.status = check_repair_order_task(rot)
            with transaction.atomic():
                repair_order.save()
                repair_order_part.save()
        self.stdout.write(f'---Successfully update parts status---')